<?php namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Http\Requests;
use App\Http\Requests\RoleCreateRequest;
use App\Http\Requests\RoleUpdateRequest;
use App\Models\Role;
use Xyb\MyPackage\Test;

class RoleController extends Controller {

	public function __construct(Role $role)
	{
		parent::__construct();
		$this->role = $role;
	}

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
        $list = $this->role->where('role_pid', '=', 0)->paginate(10);

		return view('admin/role/index', compact('list'));
	}

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create($id = null)
	{
        $parent = '';

        if($id != null)
            $parent = $this->role->select('role_name', 'id')->find($id);

        return view('admin/role/create', compact('id', 'parent'));
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store(RoleCreateRequest $request)
	{
		$this->role->create($request->all());

		\Session::flash('msg', '成功添加角色权限');

		return redirect('/admin/role/index');
	}


	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		$role = $this->role->find($id);
        $parentRoleName = '';

        if($role->role_pid != 0)
            $parentRoleName = $this->role->select('role_name')->find($role->role_pid)->role_name;

        return view('admin/role/edit', compact('role', 'parentRoleName'));
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id	 * @return Response
	 */
	public function update($id, RoleUpdateRequest $request)
	{
        $role = $this->role->find($id);

        $role->role_name = $request->input('role_name');

        $role->role_des = $request->input('role_des');

        $role->role_pid = $request->input('pid');

        $role->save();

        \Session::flash('msg', '成功修改角色权限');

        return redirect('/admin/role/index');
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		$this->role->find($id)->delete();
		\Session::flash('msg', '成功删除角色权限');
		return redirect('admin/role/index');
	}

}
